//
//  WHRPublishPostARewardVC.m
//  AnXin100
//
//  Created by 天质弘耕 on 2018/12/13.
//  Copyright © 2018 tzhg. All rights reserved.
//

#import "WHRPublishPostARewardVC.h"
#import "CZHAddressPickerView.h"
#import "zySheetPickerView.h"
#import "WHRPublishPostARewardModel.h"
#import "WHRQuestionModel.h"
#import "CCLocationManager.h"

#import "LKImagePickerView.h"

@interface WHRPublishPostARewardVC ()<UIScrollViewDelegate, UITextViewDelegate>


@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) LKImagePickerView *imagePickerView;

@property (nonatomic , strong) CZHAddressPickerView *AddressPickerView ;
/** 作物label  */
@property (nonatomic , strong) UILabel *cropLabel;
///** 作物按钮 */
@property (nonatomic , strong) UIButton *cropBtn;
/** 品种label  */
@property (nonatomic , strong) UILabel *breedLabel;
//索引
@property (nonatomic, assign) NSInteger breedIndex;
//品种数组
@property (nonatomic, strong) NSMutableArray<WHRPublishPostARewardModel*>  * breedModelArray;

/** 品种按钮 */
@property (nonatomic , strong) UILabel *breedSelectLabel;
/** 问题label  */
@property (nonatomic , strong) UILabel * problemLabel;
@property (nonatomic ,strong) NSMutableArray<WHRQuestionModel *> *problemModelArray;
/** 问题按钮 */
@property (nonatomic , strong) UILabel * problemSelectLabel;
@property (nonatomic , assign) NSInteger *questionIndex;
//名称数组
@property (nonatomic, strong) NSMutableArray  *problemArray;
/** 症状说明 */
@property (nonatomic ,strong) UILabel *explainLabel;
/** 症状说明textView */
@property (nonatomic ,strong) UITextView *explainTextView;
/** 上传照片 */
@property (nonatomic , strong) UILabel *upLabel;
/** 悬赏价格 */
@property (nonatomic , strong) UILabel *priceLabel;
@property (nonatomic , strong) UITextField *priceTextField;
@property (nonatomic , strong) UILabel *yuanLabel;
/** 确认发布 */
@property (nonatomic , strong) UIButton *sureBtn;

@property (nonatomic, assign) NSInteger currentTwoLeftIndex;
@property (nonatomic, assign) NSInteger currentTwoRightIndex;

@property (nonatomic , strong) NSString *cropId;
/** 品种id */
@property (nonatomic , strong) NSString *subsectionConId;
/** 问题ID */
@property (nonatomic , strong) NSString *fModuleId;
/** 经纬度 */
@property (nonatomic , strong) UILabel *jwdLabel;
/** 区 */
@property (nonatomic , strong) UILabel *quLabel;
/** 品种id */
@property (nonatomic, strong) NSString *pzId;
/** 问题id */
@property (nonatomic , strong) NSString *wtId;

@end

@implementation WHRPublishPostARewardVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupNavigationItems];
   
    [self creatView];
    [self setlayout];
    [self requestQuestion];
   

   
}


- (void)creatView {
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, NAVIGATION_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_HEIGHT)];
    self.scrollView.delegate = self;
    self.scrollView.userInteractionEnabled = YES;
    [self.view addSubview:self.scrollView];
    
    self.cropLabel = [[UILabel alloc]init];
    self.cropLabel.text = @"作物";
    self.cropLabel.font = FONT_FOURTEEN;
    self.cropLabel.textColor = [UIColor lightGrayColor];
    [self.scrollView addSubview:self.cropLabel];
    
    self.cropBtn = [[UIButton alloc]init];
    [self.cropBtn setTitleColor:COLOR_GREEN forState:UIControlStateNormal];
    [self.cropBtn addTarget:self action:@selector(crop:) forControlEvents:UIControlEventTouchUpInside];
    self.cropBtn.borderWidth = 1;
    self.cropBtn.cornerRadius = 2;
    self.cropBtn.borderColor = [UIColor lightGrayColor];
    [self.scrollView addSubview:self.cropBtn];
    
  
    
    self.breedLabel = [[UILabel alloc]init];
    self.breedLabel.text = @"品种";
    self.breedLabel.font = FONT_FOURTEEN;
    self.breedLabel.textColor = [UIColor lightGrayColor];
    [self.scrollView addSubview:self.breedLabel];
    

    self.problemLabel = [[UILabel alloc]init];
    self.problemLabel.text = @"问题分类";
    self.problemLabel.font = FONT_FOURTEEN;
    self.problemLabel.textColor = [UIColor lightGrayColor];
    [self.scrollView addSubview:self.problemLabel];
    
    self.breedSelectLabel = [[UILabel alloc]init];
    self.breedSelectLabel.textAlignment = NSTextAlignmentCenter;
    self.breedSelectLabel.textColor = COLOR_GREEN;
    self.breedSelectLabel.userInteractionEnabled = YES;
    self.breedSelectLabel.borderWidth = 1;
    self.breedSelectLabel.borderColor = [UIColor lightGrayColor];
    [self.scrollView addSubview:self.breedSelectLabel];
    
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Kind:)];
    [self.breedSelectLabel addGestureRecognizer:tap1];
    
    
    self.problemLabel = [[UILabel alloc]init];
    self.problemLabel.text = @"问题分类";
    self.problemLabel.font = FONT_FOURTEEN;
    self.problemLabel.textColor = [UIColor lightGrayColor];
    [self.scrollView addSubview:self.problemLabel];
    
    self.problemSelectLabel = [[UILabel alloc]init];
    self.problemSelectLabel.textAlignment = NSTextAlignmentCenter;
    self.problemSelectLabel.textColor = COLOR_GREEN;
    self.problemSelectLabel.userInteractionEnabled = YES;
    self.problemSelectLabel.borderWidth = 1;
    self.problemSelectLabel.borderColor = [UIColor lightGrayColor];
    [self.scrollView addSubview:self.problemSelectLabel];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(requestQuestion:)];
    [self.problemSelectLabel addGestureRecognizer:tap];
    
    self.explainLabel = [[UILabel alloc]init];
    self.explainLabel.text = @"症状说明";
    self.explainLabel.font = FONT_FOURTEEN;
    self.explainLabel.textColor = [UIColor lightGrayColor];
    [self.scrollView addSubview:self.explainLabel];
    
    self.explainTextView = [[UITextView alloc]init];
    self.explainTextView.font = FONT_FOURTEEN;
    self.explainTextView.textColor = COLOR_GREEN;
    self.explainTextView.cornerRadius = 2;
    self.explainTextView.borderColor = [UIColor lightGrayColor];
    self.explainTextView.borderWidth = 1;
    [self.scrollView addSubview:self.explainTextView];
    
    self.upLabel = [[UILabel alloc]init];
    self.upLabel.text = @"上传照片";
    self.upLabel.font = FONT_FOURTEEN;
    self.upLabel.textColor = [UIColor lightGrayColor];
    [self.scrollView addSubview:self.upLabel];
    
    
    NSInteger number = (SCREEN_WIDTH - 10) / 95;
    //    self.imagePickerView = [LKImagePickerView imagePickerViewWithFrame:CGRectMake(5, CGRectGetMaxY(self.upLabel.frame) + 10, SCREEN_WIDTH - 10, (SCREEN_WIDTH - 10)/number) currentViewController:self];
    self.imagePickerView = [[LKImagePickerView alloc]init];
    self.imagePickerView.currentViewController = self;
    self.imagePickerView.maxCountAtSingleLine = number;
    self.imagePickerView.maxImageCount = 9;
    
    ZX_WEAK_SELF
    
    [self.imagePickerView selectedImageCountDidChangeHandler:^(NSInteger imageCount, NSInteger itemCount, CGFloat bestHeightWidthRatio) {
        
        CGRect frame = weakSelf.imagePickerView.frame;
        frame.size.height = frame.size.width * bestHeightWidthRatio;
        weakSelf.imagePickerView.frame = frame;
        
        weakSelf.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, MAX(CGRectGetMaxY(weakSelf.imagePickerView.frame) + 5, CGRectGetHeight(weakSelf.scrollView.frame) + 1));
        
    }];
    
    self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, MAX(CGRectGetMaxY(self.imagePickerView.frame) + 5, CGRectGetHeight(self.scrollView.frame) + 1));
    
    [self.scrollView addSubview:self.imagePickerView];
   
   
    self.priceLabel = [[UILabel alloc]init];
    self.priceLabel.text = @"悬赏价格";
    self.priceLabel.font = FONT_FOURTEEN;
    self.priceLabel.textColor = [UIColor lightGrayColor];
    [self.scrollView addSubview:self.priceLabel];
    
    self.priceTextField = [[UITextField alloc]init];
    self.priceTextField.textColor = [UIColor lightGrayColor];
    self.priceTextField.borderColor = [UIColor lightGrayColor];
    self.priceTextField.borderWidth = 1;
    [self.scrollView addSubview:self.priceTextField];
    
    self.yuanLabel = [[UILabel alloc]init];
    self.yuanLabel.text = @"元";
    self.yuanLabel.font = FONT_FOURTEEN;
    self.yuanLabel.textColor = COLOR_ORG;
    [self.scrollView addSubview:self.yuanLabel];
    
    self.sureBtn = [[UIButton alloc]init];
    [self.sureBtn addTarget:self action:@selector(sure:) forControlEvents:UIControlEventTouchUpInside];
    self.sureBtn.backgroundColor = COLOR_GREEN;
    [self.sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.sureBtn setTitle:@"确认发布" forState:UIControlStateNormal];
    self.sureBtn.titleLabel.font = FONT_FOURTEEN;
    [self.scrollView addSubview:self.sureBtn];
    
    
    
}


- (void)setlayout {
    
   
    
  self.cropLabel.sd_layout
    .leftSpaceToView(self.scrollView, 10)
    .topSpaceToView(self.scrollView, 50)
    .widthIs(70)
    .heightIs(30);
    
    
    self.cropBtn.sd_layout
    .leftSpaceToView(self.cropLabel, 10)
    .topSpaceToView(self.scrollView, 50)
    .widthIs(SCREEN_WIDTH-180)
    .heightIs(30);
    
    self.breedLabel.sd_layout
    .leftSpaceToView(self.scrollView, 10)
    .topSpaceToView(self.cropLabel, 30)
    .widthRatioToView(self.cropLabel, 1)
    .heightRatioToView(self.cropLabel, 1);
    
    self.breedSelectLabel.sd_layout
    .leftSpaceToView(self.breedLabel, 10)
    .topSpaceToView(self.cropBtn, 30)
    .widthRatioToView(self.cropBtn, 1)
    .heightIs(30);
    
    self.problemLabel.sd_layout
    .leftSpaceToView(self.scrollView, 10)
    .topSpaceToView(self.breedLabel, 10)
    .widthRatioToView(self.cropLabel, 1)
    .heightRatioToView(self.cropLabel, 1);
    
    self.problemSelectLabel.sd_layout
    .leftSpaceToView(self.problemLabel, 10)
    .topSpaceToView(self.breedSelectLabel, 30)
    .widthIs(SCREEN_WIDTH-180)
    .heightIs(30);
    
    
    self.explainLabel.sd_layout
    .leftSpaceToView(self.scrollView, 10)
    .topSpaceToView(self.problemLabel, 30)
    .widthRatioToView(self.cropLabel, 1)
    .heightRatioToView(self.cropLabel, 1);
    
    self.explainTextView.sd_layout
    .leftSpaceToView(self.explainLabel, 10)
    .topSpaceToView(self.problemSelectLabel, 30)
    .widthRatioToView(self.cropBtn, 1)
    .heightIs(100);

  
    self.upLabel.sd_layout
    .leftSpaceToView(self.scrollView, 10)
    .topSpaceToView(self.explainTextView, 10)
    .widthRatioToView(self.cropLabel, 1)
    .heightRatioToView(self.cropLabel, 1);
    


    self.imagePickerView.sd_layout
    .leftSpaceToView(self.scrollView, 10)
    .topSpaceToView(self.upLabel, 10)
    .widthIs(SCREEN_WIDTH-20)
    .heightIs(100);
    
 
    
    
    self.priceLabel.sd_layout
    .leftSpaceToView(self.scrollView, 10)
    .topSpaceToView(self.imagePickerView,10)
    .widthRatioToView(self.cropLabel, 1)
    .heightRatioToView(self.cropLabel, 1);
    
    self.priceTextField.sd_layout
    .leftSpaceToView(self.priceLabel, 10)
    .topSpaceToView(self.imagePickerView, 10)
    .widthIs(100)
    .heightRatioToView(self.cropLabel, 1);
    
    self.yuanLabel.sd_layout
    .leftSpaceToView(self.priceTextField, 10)
    .topSpaceToView(self.imagePickerView,10)
    .widthRatioToView(self.cropLabel, 1)
    .heightRatioToView(self.cropLabel, 1);
    
    
    self.sureBtn.sd_layout
    .leftSpaceToView(self.scrollView, 0)
    .bottomSpaceToView(self.scrollView, 0)
    .rightSpaceToView(self.scrollView, 0)
    .heightIs(50);
    
    
}


#pragma mark - Action

/**  确认发布的方法 */

- (void)sure:(UIButton *)btn {
    
    if (self.cropBtn.titleLabel.text.length < 1) {
        
        [self showAlertViewWithMessage:@"作物不能为空！" cancelHandler:nil];
        return;
    }
    
    if (self.breedModelArray.count <1) {
       
        [self showAlertViewWithMessage:@"品种不能为空！" cancelHandler:nil];
        return;
    }
    
    
    if (self.problemModelArray.count < 1) {
        
        [self showAlertViewWithMessage:@"问题分类不能为空" cancelHandler:nil];
        return;
    }
    
    if (self.explainLabel.text.length < 1) {
        
        [self showAlertViewWithMessage:@"症状说明为空" cancelHandler:nil];
        return;
    }
    
    
    
    if (self.priceLabel.text.length < 1) {
        
        [self showAlertViewWithMessage:@"悬赏价格不能为空" cancelHandler:nil];
        return;
    }
    
    [self requestSure];
    
     [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark - undo (未完)

- (void)requestSure{
    ZX_WEAK_SELF
    /** 获取经纬度 */
    [[CCLocationManager shareLocation] getLocationCoordinate:^(CLLocationCoordinate2D locationCorrrdinate) {
        
        NSLog(@"%f %f",locationCorrrdinate.latitude,locationCorrrdinate.longitude);
        weakSelf.jwdLabel = [[UILabel alloc]init];
        [weakSelf.jwdLabel setText:[NSString stringWithFormat:@"%f,%f",locationCorrrdinate.latitude,locationCorrrdinate.longitude]];
        
    }];
    
    //获取区
    [[CCLocationManager shareLocation]getSubdivide:^(NSString *SubdivideString) {
        NSLog(@"%@",SubdivideString);
        weakSelf.quLabel = [[UILabel alloc]init];
        [weakSelf.quLabel setText:[NSString stringWithFormat:@"%@",SubdivideString]];
        
    }];
    
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
    /** 作物id */
        params[@"fCropId"] = self.cropId;
    /** 品种id */
        params[@"fCropVarietyId"] = self.pzId;
    /** 问题id */
        params[@"fQuestionType"] = self.wtId;
    /** 症状说明 */
        params[@"fSymptomInfo"]= self.explainTextView.text;
    /** 上传照片 */
        params[@"fUpImgFiles"] = self.imagePickerView.photos;
    /** 发布类型(0:发布悬赏1:直接发问专家) */
        params[@"fFlag"] = @"0";
    /** 悬赏价格 */
        params[@"fPrice"] = self.priceTextField.text;
    /** 地理信息 盘龙区 */
        params[@"fGeography"] = self.quLabel.text;
    /** 位置坐标 经纬度 */
        params[@"fCoordinate"] =self.jwdLabel.text;
    /** 用户id */
        params[@"fUserId"] = @"zzh001";
    /** 支付类型 */
        params[@"payType"] = @"1";
   
    [NetWorkHelp yl_postDataWith:params url:URL_SPLIT(URL_AgricultureCircleProfessorDiagnose, URL_AgricultureCircleProfessorDiagnose_insertStartQuestion) success:^(id respose) {
    
        if ([respose[@"header"][@"statusCode"] integerValue] == 200) {
            
            UIAlertController *alertController3 = [UIAlertController alertControllerWithTitle:@"发布成功" message:respose[@"header"][@"statusMessage"] preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                NSLog(@"点击了按钮1，进入按钮1的事件");
            }];
           
            [alertController3 addAction:action1];

            [self presentViewController:alertController3 animated:YES completion:nil];
            
        }
        
    } failure:^(NSError *error) {
        
        NSLog(@"登录失败%@",error);
    }];
    
    
    
}



/** 作物按钮 */
- (void)crop:(UIButton *)btn {
    
    ZX_WEAK_SELF
    
  _AddressPickerView  = [CZHAddressPickerView cityPickerViewWithProvince:@"" city:@"" cityBlock:^(NSString *province, NSString *city) {
        
        [self.cropBtn setTitle:[NSString stringWithFormat:@"%@%@",province,city] forState:UIControlStateNormal];
        NSLog(@"%@%@",province,city);
        
    }];
    
    _AddressPickerView.choice_CropId = ^(NSString *cropOneId, NSString *cropTwoId) {
        
        weakSelf.cropId = cropTwoId;
        
        NSLog(@"%@%@",cropOneId,cropTwoId);
    };
}




/** 品种 */
- (void)Kind:(UIButton *)btn {
    
    /** 请求品种 */
     [self requestBreed];
    
  
    
}

/** 问题*/

- (void)requestQuestion:(UITapGestureRecognizer *)tap {
    
      ZX_WEAK_SELF
    
    NSMutableArray<NSString *>  *mArray = [NSMutableArray<NSString *> array ];
    for (WHRQuestionModel *model in self.problemModelArray) {
        
        NSString *str = model.fName;
        /** 问题ID */
        self.wtId = model.fId;
        [mArray addObject:str];
    }
  
    zySheetPickerView *pickerView = [zySheetPickerView ZYSheetStringPickerWithTitle:mArray  andHeadTitle:@"选择问题" Andcall:^(zySheetPickerView *pickerView, NSString *choiceString) {
        
      
        weakSelf.problemSelectLabel.text = choiceString;
        [pickerView dismissPicker];
    }];
    [pickerView show];
    
}




- (void)breedBtn:(UIButton *)btn {
    
    
    
}


#pragma mark - requestNetwork

/** 品种 */
- (void)requestBreed{
    
    ZX_WEAK_SELF
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"cropColumnBase"] = @"zppz";
    params[@"cropId"] = self.cropId;
 
    [NetWorkHelp yl_postDataWith:params url:URL_SPLIT(URL_AgricultureCircle, URL_AgricultureCircle_findIllnessByCroId) success:^(id respose) {
    
        if ([respose[@"header"][@"statusCode"] integerValue] == 200) {
            
            weakSelf.breedModelArray = [WHRPublishPostARewardModel mj_objectArrayWithKeyValuesArray:respose[@"body"][@"cropSubsectionConList"]];
           
            NSMutableArray<NSString *>  *mArray = [NSMutableArray<NSString *> array ];
            
            for (WHRPublishPostARewardModel *model in self.breedModelArray) {
                
                NSString *str = model.name;
                /** 品种id */
                self.pzId = model.subsectionConId;
                [mArray addObject:str];
            }
            zySheetPickerView *pickerView = [zySheetPickerView ZYSheetStringPickerWithTitle:mArray  andHeadTitle:@"选择问题" Andcall:^(zySheetPickerView *pickerView, NSString *choiceString) {
                
                weakSelf.breedSelectLabel.text = choiceString;
                
                [pickerView dismissPicker];
            }];
            [pickerView show];
           
            
        } else if ([respose[@"header"][@"statusCode"] integerValue] != 200) {
            
        }
        
        
    } failure:^(NSError *error) {
        
        
        
    }];
    
    
    
}

/** 问题 */

- (void)requestQuestion {
    
    ZX_WEAK_SELF
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"fCode"] = @"zwwtfl";
    
    [NetWorkHelp yl_postDataWith:params url:URL_SPLIT(URL_AgricultureCircleCrop, URL_AgricultureCircleCrop_selectDictionary) success:^(id respose) {
        
        if ([respose[@"header"][@"statusCode"] integerValue] == 200) {
            
          
            
            weakSelf.problemModelArray = [WHRQuestionModel mj_objectArrayWithKeyValuesArray:respose[@"body"][@"dictionaryModuleDetailList"]];
            
            
            
        } else if ([respose[@"header"][@"statusCode"] integerValue] != 200) {
            
        }
        
        
    } failure:^(NSError *error) {
        
        
    }];
    
}



- (void)setupNavigationItems {
    
    /** 导航条左边的按钮 */
    //返回按钮
    UIButton *kfBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [kfBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    kfBtn.frame = CGRectMake(0, 0, 30, 30);
    [kfBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:kfBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    /** 搜索 */
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    titleLabel.text = @"发布悬赏";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = FONT_EIGHTEEN;
    titleLabel.textColor = [UIColor colorWithHex:0x29b566];
    self.navigationItem.titleView = titleLabel;
    
    
}


- (void)backAction:(UIBarButtonItem *)item {
    
    [self.navigationController popViewControllerAnimated:YES];
}






/** 品种数组 */
- (NSMutableArray<WHRPublishPostARewardModel *> *)breedModelArray {
    
    if (!_breedModelArray) {
        
        _breedModelArray = [NSMutableArray<WHRPublishPostARewardModel *> array];
    }
    return _breedModelArray;
}



/** 问题数组*/

- (NSMutableArray<WHRQuestionModel *> *)problemModelArray {
    
    if (!_problemModelArray) {
        
        _problemModelArray = [NSMutableArray<WHRQuestionModel *> array];
    }
    
    return _problemModelArray;
    
}

#pragma mark - ScrollView Delegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView == self.scrollView) {
        [self.view endEditing:YES];
    }
}




//点击空白处收起键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.view endEditing:YES];
}



@end
